﻿#region

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

#endregion

namespace Pob.GameObjects
{
    public class FpsCounter : DrawableGameComponent
    {
        #region Fields

        private readonly string _asset;

        private float _elapsed;

        private SpriteFont _font;

        private float _frameRate;

        private float _frames;

        private SpriteBatch _spriteBatch;

        #endregion

        #region Constructors and Destructors

        public FpsCounter(Game game, string asset)
            : base(game)
        {
            _elapsed = 0.0f;
            _frameRate = 0.0f;
            _frames = 0.0f;
            _asset = asset;
        }

        #endregion

        #region Public Methods and Operators

        public override void Draw(GameTime gameTime)
        {
            _elapsed += (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (_elapsed > 1.0f)
            {
                _elapsed -= 1.0f;
                _frameRate = _frames;
                _frames = 0;
            }
            else
            {
                _frames += 1;
            }

            _spriteBatch.Begin();
            _spriteBatch.DrawString(_font, _frameRate.ToString("0"), new Vector2(10, 10), Color.White);
            _spriteBatch.End();
        }

        #endregion

        #region Methods

        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);
            _font = Game.Content.Load<SpriteFont>(_asset);
        }

        #endregion
    }
}